var AlMap = function () {
    var ALAGISMAP = null,
        BAIDUMAP = null;
    const startZoom =16;

    // MapSwitch2D || MapSwitchWX || MapSwitch3D
    var currentMapType = 'MapSwitch3D';
    const currentCity = '营口市';

     

    function initAlagisMap(pointArray, zoom) {
        if (arguments.length === 0) {
            pointArray = [122.2141, 40.6586];
            zoom = startZoom
        }
        var c = alagis.proj.transform(pointArray, 'EPSG:4326', 'EPSG:3857');
        var center = alagis.util.etoMercator(c);
        let view =  new alagis.View({
            center: center,
            zoom: zoom,
            maxZoom: 20,
            minZoom: 14
        });
        switchDisplayMap();
        if (ALAGISMAP == null) {
            
            ALAGISMAP = new alagis.Map({
                controls: alagis.control.defaults().extend([
                     new alagis.control.MPEdushi({
                        targetID: "location",
                        pix: 6
                    }), new alagis.control.ZoomSlider(), new alagis.control.ZoomToExtent({
                        extent: [
                            13807525.128593057, 3791805.7784746373, 13808033.315398298,
                            3792182.5891375123
                        ]
                    })
                ]),
                layers: [
                    new alagis.layer.Tile({
                        preload: Infinity,
                        source: new alagis.source.EdushiMap({
                            http: 'http://120.27.215.107:8081/tile',
                            key: 'your key'
                        })
                    })
                ],
                target: 'map3D',
                view: view

            });
            AlagisMap.initAlagisMapEvent(ALAGISMAP);
        } else {
            ALAGISMAP.setView(view);
        }


    }

    function initBaiduMap(mapType,point, zoom) {
        if (BAIDUMAP == null) {
            if(!point || !zoom){
                throw new ReferenceError('坐标和缩放级别未初始化');
            }
            BAIDUMAP = new BMap.Map("mapBd"); // 创建Map实例
            // 添加带有定位的导航控件
            var navigationControl = new BMap.NavigationControl({
                // 靠左上角位置
                anchor: BMAP_ANCHOR_TOP_LEFT,
                // LARGE类型
                type: BMAP_NAVIGATION_CONTROL_LARGE
                
            });
            BAIDUMAP.addControl(navigationControl);
            BAIDUMAP.centerAndZoom(point, zoom); // 初始化地图,设置中心点坐标和地图级别
            if(mapType){
                BAIDUMAP.setMapType(mapType);
            }
            BAIDUMAP.setCurrentCity(currentCity); // 设置地图显示的城市 此项是必须设置的
            BAIDUMAP.enableScrollWheelZoom(true); //开启鼠标滚轮缩放
            // 覆盖区域图层测试
            BAIDUMAP.addTileLayer(new BMap.PanoramaCoverageLayer());

            var stCtrl = new BMap.PanoramaControl(); //构造全景控件
            stCtrl.setOffset(new BMap.Size(37, 200));
            BAIDUMAP.addControl(stCtrl);//添加全景控件
        } else {
            if(point&&zoom){
                BAIDUMAP.centerAndZoom(point, zoom);
            }
            if(mapType){
                BAIDUMAP.setMapType(mapType);
            }
        }

    }

    function switchDisplayMap(targetMapType) {
        $('.ag-map').removeClass('ag-map-zindex-max');
        if(targetMapType == 'MapSwitch2D' || targetMapType == 'MapSwitchWx'){
            $('#mapBd').addClass('ag-map-zindex-max');
        }else{
            $('#map3D').addClass('ag-map-zindex-max');
        }
    }

    function switchMap() {
        $(".MapSwitchControl ul li").click(function () {
            var targetMapType = $(this).find("div").attr("id");
            var centerPoint = null;
            var currentZoom = null;
            if (currentMapType === targetMapType) {
                return false;
            }
            $(".MapSwitchControl .current").removeClass("current");
            $(this).find("div").addClass("current");
            switchDisplayMap(targetMapType);
            if (currentMapType == 'MapSwitch2D' && targetMapType == 'MapSwitchWx') {
                initBaiduMap(BMAP_HYBRID_MAP);
            } else if (currentMapType == 'MapSwitch2D' && targetMapType == 'MapSwitch3D') {
                centerPoint = alagisUtil.baiduToEdushi(BAIDUMAP.getCenter());
                currentZoom = BAIDUMAP.getZoom();
                initAlagisMap(centerPoint,currentZoom);
            } else if (currentMapType == 'MapSwitchWx' && targetMapType == 'MapSwitch2D') {
                initBaiduMap(BMAP_NORMAL_MAP);
            } else if (currentMapType == 'MapSwitchWx' && targetMapType == 'MapSwitch3D') {
                centerPoint = alagisUtil.baiduToEdushi(BAIDUMAP.getCenter());
                currentZoom = BAIDUMAP.getZoom();
                initAlagisMap(centerPoint,currentZoom);
            } else if (currentMapType == 'MapSwitch3D' && targetMapType == 'MapSwitch2D') {
                centerPoint = alagisUtil.edushiToBaidu(ALAGISMAP.getView().getCenter());
                currentZoom = ALAGISMAP.getView().getZoom();
                initBaiduMap(BMAP_NORMAL_MAP,centerPoint,currentZoom);
            } else if (currentMapType == 'MapSwitch3D' && targetMapType == 'MapSwitchWx') {
                centerPoint = alagisUtil.edushiToBaidu(ALAGISMAP.getView().getCenter());
                currentZoom = ALAGISMAP.getView().getZoom();
                initBaiduMap(BMAP_HYBRID_MAP,centerPoint,currentZoom);
            }
            currentMapType = targetMapType;
        });
    }



    return {
        initMap: function () {
            initAlagisMap();
            switchMap();
        }
    }
}();